﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using nplJobsBLL.Common;
using nplJobsDAL.Common;
using nplJobsCommon.JobSeeker;
using nplJobsCommon.Employeer;
using nplJobsBLL.ProcessSelectJobSeeker;
using nplJobsBLL.ProcessEmployeer;

public partial class EmailConfirmation : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] == null)
        {
            Response.Redirect("Default.aspx");
        };

        if (!this.IsPostBack)
        {
            Guid newUserId = Guid.Parse(Request.QueryString["ID"].ToString());
            MembershipUser newUser = Membership.GetUser(newUserId);
            if (newUser == null)
            {
                lblMessage.Text = "User Account not found";
                HideControls(true);
            }
            else if (newUser.IsApproved == false)
            {
                newUser.IsApproved = true;
                Membership.UpdateUser(newUser);

                HideControls(false);
                ViewState["CONFIRMATION_USER"] = newUser;
            }
            else if (newUser.IsApproved)
            {
                lblMessage.Text = "Account already approved";
                HideControls(true);
            }
        }
    }

    protected void lnkLogin_Click(object sender, EventArgs e)
    {
        if (ViewState["CONFIRMATION_USER"] != null)
        {
            MembershipUser newUser = (MembershipUser)ViewState["CONFIRMATION_USER"];

            ProcessTblUserType processTblUserType = new ProcessTblUserType();
            processTblUserType.UserId = (Guid)newUser.ProviderUserKey;
            processTblUserType.invoke();
            tblUserType tblUserType = processTblUserType.tblUserType;
            if (tblUserType == null) return;

            if (tblUserType.UserType == 1) //Job seeker
            {
                JobSeeker jobSeeker = GetJobSeeker(processTblUserType.UserId);
                if (jobSeeker != null)
                {
                    Session["UserID"] = jobSeeker.JobSeekerID;
                    FormsAuthentication.SetAuthCookie(newUser.UserName, true);

                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, newUser.UserName, DateTime.Now, DateTime.Now.AddDays(1), false,
                    "JobSeekerData", FormsAuthentication.FormsCookiePath);

                    string encTicket = FormsAuthentication.Encrypt(ticket);
                    HttpCookie hc = new HttpCookie("AuthCookJobSeeker", encTicket);
                    hc.Expires = ticket.Expiration;
                    HttpContext.Current.Response.Cookies.Add(hc);

                    HttpContext.Current.Response.Cookies["JobSeekerUserName"].Value = newUser.UserName;
                    HttpContext.Current.Response.Cookies["JobSeekerPassword"].Value = string.Empty;
                    bool isRemembered = false;
                    HttpContext.Current.Response.Cookies["JobSeekerRememberMe"].Value = isRemembered.ToString();

                    {
                        HttpContext.Current.Response.Cookies["JobSeekerUserName"].Expires = DateTime.Now.AddDays(-1);
                        HttpContext.Current.Response.Cookies["JobSeekerPassword"].Expires = DateTime.Now.AddDays(-1);
                        HttpContext.Current.Response.Cookies["JobSeekerRememberMe"].Expires = DateTime.Now.AddDays(-1);
                    }

                    Session["MoveToHomePage"] = "false";
                    Session["userInfo"] = newUser;
                    Session["jobseeker"] = jobSeeker;
                    Session["user"] = General.Users.JobSeeker.ToString();
                    Session["JOBSEEKERSUCCESSFULLLOGGED"] = true;
                }
            }
            else //Employeer
            {
                Employeer employeer = GetEmployeer(processTblUserType.UserId);
                if (employeer != null)
                {
                    FormsAuthentication.SetAuthCookie(newUser.UserName, true);

                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, newUser.UserName, DateTime.Now, DateTime.Now.AddDays(1), false,
                          "EmployeerData", FormsAuthentication.FormsCookiePath);
                    string encTicket = FormsAuthentication.Encrypt(ticket);
                    HttpCookie hc = new HttpCookie("AuthCookEmp", encTicket);
                    hc.Expires = ticket.Expiration;
                    HttpContext.Current.Response.Cookies.Add(hc);

                    HttpContext.Current.Response.Cookies["EmpUserName"].Value = newUser.UserName;
                    HttpContext.Current.Response.Cookies["EmpPassword"].Value = string.Empty;
                    HttpContext.Current.Response.Cookies["EmpRememberMe"].Value = "";

                    {
                        HttpContext.Current.Response.Cookies["EmpUserName"].Expires = DateTime.Now.AddDays(-1);
                        HttpContext.Current.Response.Cookies["EmpPassword"].Expires = DateTime.Now.AddDays(-1);
                        HttpContext.Current.Response.Cookies["EmpRememberMe"].Expires = DateTime.Now.AddDays(-1);
                    }
                    Session["userInfo"] = newUser;
                    Session["user"] = General.Users.Employeer.ToString();
                    //Session["employeer"] = employeer;
                    Session["SUCCESSFULLLOGGED"] = true;
                }
            }
            Response.Redirect("Default.aspx");
        }
    }

    private void HideControls(bool b)
    {
        lblMessage.Visible = b;
        pnlLogin.Visible = !b;
    }

    private JobSeeker GetJobSeeker(Guid jobSeekerId)
    {
        JobSeeker jobSeeker = new JobSeeker();
        ProcessSelectJobSeeker selectJobSeeker = new ProcessSelectJobSeeker();

        selectJobSeeker.JobSeekerID = jobSeekerId;
        selectJobSeeker.invoke();

        if (selectJobSeeker.JobSeeker != null)
        {
            jobSeeker = selectJobSeeker.JobSeeker.FirstOrDefault();
        }

        return jobSeeker;
    }

    private Employeer GetEmployeer(Guid employeerId)
    {
        Employeer employeer = new Employeer();
        ProcessSelectEmployeer selectEmployeer = new ProcessSelectEmployeer();

        selectEmployeer.employeerID = employeerId;
        selectEmployeer.invoke();

        if (selectEmployeer.Employeer != null)
        {
            employeer = selectEmployeer.Employeer.FirstOrDefault();
        }

        return employeer;
    }
}
